<?php

/**
 * anadirContrato actions.
 *
 * @package    saveenergy
 * @subpackage anadirContrato
 * @author     0
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class anadirContratoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //$this->form = new subirExcelForm();

  }

  public function executeSubirExcel(sfWebRequest $request)
  {
    $this->form = new subirExcelForm();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('excel'), $request->getFiles('excel'));
      if ($this->form->isValid())
        {
          $file = $this->form->getValue('archivo');
          $filename = 'contrato2_'.sha1($file->getOriginalName());
          $extension = $file->getExtension($file->getOriginalExtension());
          $file->save($filename.$extension);

          //Meter esta ruta en variable(Buscar)
//          $this->data = new Spreadsheet_Excel_Reader(sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR."contratos".DIRECTORY_SEPARATOR.$filename.$extensions);
          return $this->redirect('anadirContrato/new?nombreContrato='.$filename.$extension);

        }
    }
  }

  public function executeSubmit(sfWebRequest $request)
  {
//    $this->form = new ContactForm();
//
//    if ($request->isMethod('post'))
//    {
//      $this->form->bind($request->getParameter('contact'), $request->getFiles('contact'));
//      if ($this->form->isValid())
//        {
//          $file = $this->form->getValue('file');
//
//          $filename = 'uploaded_'.sha1($file->getOriginalName());
//          $extension = $file->getExtension($file->getOriginalExtension());
//          $file->save(sfConfig::get('sf_upload_dir').'/contratos/'.$filename.$extension);
//          return $this->redirect('anadirContrato/thankyou');
//        }
//    }
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->contrato);
  }

  public function executeNew(sfWebRequest $request)
  {
      $this->nombreContrato=$request->getParameter('nombreContrato');//Nombre que le paso por paremetros en la url para la ubicacion

      $this->form = new anadirContratoForm();
//    $this->form = new anadirContratoForm(array('referencia', 'Aqui van los atributos', 'titular', 'Para mi es como si estuviese muerto'));
      
      $this->data = new Spreadsheet_Excel_Reader(sfConfig::get('sf_upload_dir').'/contratos/'.$this->nombreContrato);

      //Pruebas
      //$this->redirect('sfExcelReaderExample/indexSuccess?nombreContrato='.$filename.$extension);


      /*$this->nombreCompania= $this->data->val(5, 'A');
      $this->nombreEmpresa= $this->data->val(14, 'H');

      //Podemos poner desde aqui los valores por defecto del formulario
      $this->form->setDefault('referencia',$this->nombreCompania);
      $this->form->setDefault('titular',$this->nombreEmpresa);
  */

  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ContratoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id'))), sprintf('Object contrato does not exist (%s).', $request->getParameter('id')));
    
    $this->form = new ContratoForm($contrato);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id'))), sprintf('Object contrato does not exist (%s).', $request->getParameter('id')));
    $this->form = new ContratoForm($contrato);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id'))), sprintf('Object contrato does not exist (%s).', $request->getParameter('id')));
    $contrato->delete();

    $this->redirect('anadirContrato/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    if ($form->isValid())
    {
      $contrato = $form->save();

      $this->redirect('anadirContrato/edit?id='.$contrato->getId());
    }
  }



    public function executeThankyou()
  {
  }
}
